$(document).ready(function (){
  var grades = new Array();
  var countUser = 0;
  var countNologin = 0;
  $('#dg').datagrid({
      url:'dullGetAllClass',
      columns:[[
          {field:'gname',checkbox: true},
          {field:'schoolname',title:'学校名称',width:230},
          {field:'gradename',title:'班级',width:120},
          {field:'nologin',title:'未登录人数',width:150},
          {field:'alluser',title:'所有用户',width:200},
      ]],
      pagination:false,
      toolbar: '#tb',
      showHeader: true,
      singleSelect: false,
      showFooter: true,
      rownumbers: true,
      onCheck: function (index,field,value){
        grades[index] = field.gradename;
        countNologin = parseInt(countNologin)+parseInt(field.nologin);
        countUser = parseInt(countUser)+parseInt(field.alluser);
        changeTotal(countNologin,countUser)
      },
      onUncheck: function (index,field,value){
        delete(grades[index])
        countNologin = parseInt(countNologin)-parseInt(field.nologin);
        countUser = parseInt(countUser)-parseInt(field.alluser);
        changeTotal(countNologin,countUser)
      },
      onCheckAll: function (index){
        grades = [];
        countNologin = countUser = 0;
        for(key in index){
          grades.push(index[key].gradename)
          countNologin = parseInt(countNologin)+parseInt(index[key].nologin);
          countUser = parseInt(countUser)+parseInt(index[key].alluser);
          changeTotal(countNologin,countUser)
        }
        console.log(grades)
      },
      onUncheckAll: function (index){
        grades = []
        countNologin = 0;
        countUser = 0;
        changeTotal(countNologin,countUser)
        console.log(grades)
      },
      onLoadSuccess: function (){
        $('.datagrid-header-check input').click();
      }
  });

  $('#search').click(function() {
      var mintime = $("input[name='mintime']").val();
      var maxtime = $("input[name='maxtime']").val();
      grades = [];
      $('.panel-title').text(mintime+' 至 '+maxtime+' 未登录');
      $('#dg').datagrid('reload', {
          mintime: mintime,
          maxtime: maxtime,
      });
  });

  $('#detaillist').click(function() {
    var mintime = $("input[name='mintime']").val();
    var maxtime = $("input[name='maxtime']").val();
    $('#detail').datagrid('reload',{
      mintime: mintime,
      maxtime: maxtime,
      grades: grades.join(),
      total: countNologin,
    })
  });

  $('#detail').datagrid({
      url:'dullGetNameList',
      columns:[[
          {field:'schoolname',title:'学校名称',width:200},
          {field:'gradename',title:'班级',width:100},
          {field:'username',title:'姓名',width:100},
          {field:'logonname',title:'登录名',width:150},
          {field:'devicenumber',title:'设备号',width:150},
          {field:'model',title:'设备名称',width:90},
      ]],
      pagination:true,
      pageSize: 50,
      showHeader: true,
      singleSelect: false,
  });

  function changeTotal(countNologin,countUser){
    $('.datagrid-footer .datagrid-cell-c3-nologin').text(countNologin)
    $('.datagrid-footer .datagrid-cell-c3-alluser').text(countUser)
  }


})
